﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "ElementUIInputNumber.h"
#include "Widget/SInputNumber.h"

UElementUIInputNumber::UElementUIInputNumber()
{
	NumberText = FText::FromString("0.0");
	NumberStep = 1.0;
	StepStrictly = false;
	Disabled = false;
	MinValue = FString("");
	MaxValue = FString("");
	LeftButtonWidth = 50;
	RightButtonWidth = 50;
	FButtonStyle* Temp = new FButtonStyle(FCoreStyle::Get().GetWidgetStyle<FButtonStyle>("Button"));
	LeftButtonStyle = *Temp;
	Temp = new FButtonStyle(FCoreStyle::Get().GetWidgetStyle<FButtonStyle>("Button"));
	RightButtonStyle = *Temp;
}

void UElementUIInputNumber::SynchronizeProperties()
{
	Super::SynchronizeProperties();
	// TAttribute<FText> NumberTextBinding = PROPERTY_BINDING(FText, NumberText);

	InputNumberSlate->SetOnLeftButtonClicked(BIND_UOBJECT_DELEGATE(FOnClicked, OnLeftButtonClickedHandle));
	InputNumberSlate->SetOnRightButtonClicked(BIND_UOBJECT_DELEGATE(FOnClicked, OnRightButtonClickedHandle));
	InputNumberSlate->SetMinValue(MinValue);
	InputNumberSlate->SetMaxValue(MaxValue);
	InputNumberSlate->SetNumberText(NumberText);
	InputNumberSlate->SetNumberFont(NumberFont);
	InputNumberSlate->SetLeftButtonWidth(FOptionalSize(LeftButtonWidth));
	InputNumberSlate->SetRightButtonWidth(FOptionalSize(RightButtonWidth));
	InputNumberSlate->SetLeftButtonStyle(&LeftButtonStyle);
	InputNumberSlate->SetRightButtonStyle(&RightButtonStyle);
}

FText UElementUIInputNumber::GetNumberText()
{
	if (InputNumberSlate.IsValid())
	{
		return InputNumberSlate->GetNumberText();
	}
	return FText();
}

bool UElementUIInputNumber::SetNumberText(FText NewText)
{
	if (InputNumberSlate.IsValid())
	{
		bool Result = InputNumberSlate->SetNumberText(NewText);
		if (Result)
		{
			NumberText = NewText;
		}
		return Result;
	}
	return false;
}

bool UElementUIInputNumber::SetStepStrictly(bool value)
{
	StepStrictly = value;
	InputNumberSlate->SetStepStrictly(StepStrictly);
	return true;
}

bool UElementUIInputNumber::SetDisabled(bool value)
{
	Disabled = value;
	InputNumberSlate->SetIsReadOnly(Disabled);
	return true;
}

bool UElementUIInputNumber::SetNumberStep(float Value)
{
	if (InputNumberSlate.IsValid())
	{
		if (InputNumberSlate->SetNumberStep(NumberStep))
		{
			NumberStep = Value;
			return true;
		}
	}
	return false;
}

bool UElementUIInputNumber::SetMinValue(FString NewMinValue)
{
	if (InputNumberSlate.IsValid())
	{
		bool Result = InputNumberSlate->SetMinValue(NewMinValue);
		if (Result)
		{
			MinValue = NewMinValue;
		}
		return Result;
	}
	return false;
}

bool UElementUIInputNumber::SetMaxValue(FString NewMaxValue)
{
	if (InputNumberSlate.IsValid())
	{
		bool Result = InputNumberSlate->SetMaxValue(NewMaxValue);
		if (Result)
		{
			MaxValue = NewMaxValue;
		}
		return Result;
	}
	return false;
}

bool UElementUIInputNumber::SetLeftButtonWidth(float NewValue)
{
	if (InputNumberSlate.IsValid())
	{
		bool Result = InputNumberSlate->SetLeftButtonWidth(NewValue);
		if (Result)
		{
			LeftButtonWidth = NewValue;
		}
		return Result;
	}
	return false;
}

bool UElementUIInputNumber::SetRightButtonWidth(float NewValue)
{
	if (InputNumberSlate.IsValid())
	{
		bool Result = InputNumberSlate->SetRightButtonWidth(NewValue);
		if (Result)
		{
			RightButtonWidth = NewValue;
		}
		return Result;
	}
	return false;
}

bool UElementUIInputNumber::SetLeftButtonStyle(FButtonStyle NewValue)
{
	if (InputNumberSlate.IsValid())
	{
		LeftButtonStyle = NewValue;
		InputNumberSlate->SetLeftButtonStyle(&NewValue);
		return true;
	}
	return false;
}

bool UElementUIInputNumber::SetRightButtonStyle(FButtonStyle NewValue)
{
	if (InputNumberSlate.IsValid())
	{
		RightButtonStyle = NewValue;
		InputNumberSlate->SetRightButtonStyle(&NewValue);
		return true;
	}
	return false;
}

FReply UElementUIInputNumber::OnLeftButtonClickedHandle()
{
	float TempNumber = FCString::Atof(*GetNumberText().ToString());
	TempNumber = TempNumber - NumberStep;
	SetNumberText(FText::FromString(FString::SanitizeFloat(TempNumber)));
	OnLeftButtonClicked.Broadcast();
	return FReply::Handled();
}

FReply UElementUIInputNumber::OnRightButtonClickedHandle()
{
	float TempNumber = FCString::Atof(*GetNumberText().ToString());
	TempNumber = TempNumber + NumberStep;
	SetNumberText(FText::FromString(FString::SanitizeFloat(TempNumber)));
	OnRightButtonClicked.Broadcast();
	return FReply::Handled();
}

TSharedRef<SWidget> UElementUIInputNumber::RebuildWidget()
{
	InputNumberSlate = SNew(SInputNumber).Font(NumberFont).Text(NumberText).NumberStep(NumberStep).MinValue(MinValue).MaxValue(MaxValue).LeftButtonStyle(&LeftButtonStyle).RightButtonStyle(&RightButtonStyle).StepStrictly(StepStrictly).OnNumberTextException(BIND_UOBJECT_DELEGATE(FOnNumberTextException, HandleOnNumberTextException)).OnNumberTextChanged(BIND_UOBJECT_DELEGATE(FOnTextChanged, HandleOnNumberTextChanged)).IsReadOnly(Disabled);

	return InputNumberSlate.ToSharedRef();
}

void UElementUIInputNumber::HandleOnNumberTextChanged(const FText& InText)
{
	OnNumberTextChanged.Broadcast(InText);
}

void UElementUIInputNumber::HandleOnNumberTextException(const FText& InText, EInputNumberException Exception)
{
	OnOnNumberTextException.Broadcast(InText, Exception);
}

// #if WITH_EDITOR
// 
// TSharedRef<SWidget> UElementUIInputNumber::RebuildDesignWidget(TSharedRef<SWidget> Content)
// {
// 	return Content;
// }
// 
// #endif
